package apiroute

import (
	"git.sr.ht/~mil/mobroute/src/db"
	"git.sr.ht/~mil/mobroute/src/utilfuncs"
)

func formatRouteResponse(
	diagnosticsPerf *DiagnosticsPerf,
	connectionsVerbose *[]db.ConnectionVerbose,
	transfers *[]db.Transfer,
	routeParams *RouteParams,
) *RouteResponse {
	r := RouteResponse{}

	routeLegs := formatRouteLegs(
		connectionsVerbose,
		transfers,
		*routeParams.WalkspeedKmHr,
		*routeParams.From,
		*routeParams.To,
		*routeParams.DebugFormatting,
	)

	if utilfuncs.ItemInArray("route_legs", *routeParams.OutputFormats) {
		r.RouteLegs = routeLegs
	}
	if utilfuncs.ItemInArray("geojson", *routeParams.OutputFormats) {
		r.GeoJSON = formatGeoJSON(routeLegs)
	}
	if utilfuncs.ItemInArray("mapurl", *routeParams.OutputFormats) {
		r.MapURL = ptr(formatMapURL(formatGeoJSON(routeLegs)))
	}
	if utilfuncs.ItemInArray("diagnostics_route", *routeParams.OutputFormats) {
		r.DiagnosticsRoute = formatDiagnosticsRoute(routeLegs)
	}
	if utilfuncs.ItemInArray("diagnostics_perf", *routeParams.OutputFormats) {
		r.DiagnosticsPerf = diagnosticsPerf
	}

	return &r
}
